import CryptoJS from 'crypto-js/crypto-js';
import JsEncrypt from 'jsencrypt/bin/jsencrypt';

export default {
    /**
     * 3DES加密
     * @param {*} password
     * @param {*} data
     */
    tripleDESEncrypt(password, data) {
        let encrypted = CryptoJS.TripleDES.encrypt(data, CryptoJS.enc.Utf8.parse(password), {
            iv: CryptoJS.enc.Utf8.parse('01234567'),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });

        return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
    },

    /**
     * 3DES解密
     * @param {*} password
     * @param {*} data
     */
    tripleDESDecrypt(password, data) {
        let encrypted = CryptoJS.TripleDES.decrypt(data, CryptoJS.enc.Utf8.parse(password), {
            iv: CryptoJS.enc.Utf8.parse('01234567'),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });

        return CryptoJS.enc.Utf8.stringify(encrypted);
    },

    /**
     * RSA公钥加密
     * @param {*} publicKey
     * @param {*} data
     */
    rsaEncrypt(publicKey, data) {
        let jse = new JSEncrypt();
        jse.setPublicKey(publicKey);
        return jse.encrypt(data)
    },

    /**
     * 获取随机数
     * @param {*} length
     */
    getRandom(length) {
        return CryptoJS.lib.WordArray.random(length).toString();
    }
}
